package com.UnionPayCenter.operation.controller;

import com.UnionPayCenter.entity.MpIndexPageEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.request.BaseRequest;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.operation.entity.request.GetFormIdRequest;
import com.UnionPayCenter.operation.entity.request.MpPushRequest;
import com.UnionPayCenter.operation.entity.request.MpPushToAllRequest;
import com.UnionPayCenter.operation.entity.vo.MpIndexPageVO;
import com.UnionPayCenter.service.IWechatService;
import com.UnionPayCenter.util.Asserts;
import com.UnionPayCenter.util.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 消息推送controller
 */
@Slf4j
@Api(value = "消息推送", tags = "消息推送")
@CurrentManager
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IWechatService wechatService;

    @CurrentShop
    @RequestMapping(value = "/mp/getIndexPages", method = RequestMethod.POST)
    @ApiOperation(tags = "消息推送", httpMethod = "POST", value = "[后台]获取小程序跳转页面地址列表", response = MpIndexPageVO.class)
    public ResponseWrapper getMpIndexPages(@ApiParam @RequestBody(required = false) BaseRequest baseRequest) {
        List<MpIndexPageEntity> mpIndexPageEntities = wechatService.getAllMpIndexPage();
        return ResponseWrapper.succeed(BeanUtils.transform(mpIndexPageEntities, MpIndexPageVO.class));
    }

    @CurrentShop
    @RequestMapping(value = "/getFormid", method = RequestMethod.POST)
    @ApiOperation(tags = "消息推送", httpMethod = "POST", value = "[后台]获取formId", response = String.class)
    public ResponseWrapper getFormid(@ApiParam @RequestBody(required = false) GetFormIdRequest getFormIdRequest) {
        Asserts.notNull(getFormIdRequest, "查询条件");
        Asserts.notNull(getFormIdRequest.getUserId(), "用户Id");
        return ResponseWrapper.succeed(wechatService.getFormid(getFormIdRequest.getUserId(), false));
    }

    @CurrentShop
    @RequestMapping(value = "/mp/pushMessage", method = RequestMethod.POST)
    @ApiOperation(tags = "消息推送", httpMethod = "POST", value = "[后台]推送消息", response = Boolean.class)
    public ResponseWrapper pushMessage(@ApiParam @RequestBody(required = false) MpPushRequest mpPushRequest) {
        Asserts.notNull(mpPushRequest, "推送入参");
        Asserts.notBlank(mpPushRequest.getMessage(), "消息内容");
        return ResponseWrapper.succeed(wechatService.pushMpMsg(mpPushRequest));
    }

    @CurrentShop
    @RequestMapping(value = "/mp/pushMessageToAll", method = RequestMethod.POST)
    @ApiOperation(tags = "消息推送", httpMethod = "POST", value = "[后台]推送消息", response = Boolean.class)
    public ResponseWrapper pushMessageToAll(@ApiParam @RequestBody(required = false) MpPushToAllRequest mpPushRequest) {
        Asserts.notNull(mpPushRequest, "推送入参");
        Asserts.notBlank(mpPushRequest.getMessage(), "消息内容");
        return ResponseWrapper.succeed(wechatService.pushMpMsgToAll(mpPushRequest));
    }
}
